package com.tadpole.music.view.fragment.question;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.base.TabAdapter;
import com.tadpole.music.adapter.question.HeadAdapter;
import com.tadpole.music.adapter.question.PracticeAdapter;

import com.tadpole.music.bean.question.QuestionRankBean;
import com.tadpole.music.bean.question.SimulationBean;
import com.tadpole.music.bean.question.SpecialPracticeBean;
import com.tadpole.music.bean.question.UniversityBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.question.QuestionRankIView;
import com.tadpole.music.iView.question.SimulationIView;
import com.tadpole.music.iView.question.SpecialPracticeIView;
import com.tadpole.music.presenter.question.QuestionRankPresenter;
import com.tadpole.music.presenter.question.SimulationPresenter;
import com.tadpole.music.presenter.question.SpecialPracticePresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.activity.question.FreeExperienceActivity;
import com.tadpole.music.view.activity.question.RealTrainActivity;
import com.tadpole.music.view.activity.question.SimulationExerciseActivity;
import com.tadpole.music.view.activity.question.SimulationPracticeActivity;
import com.tadpole.music.view.activity.question.SpecialExercisesActivity;
import com.tadpole.music.view.fragment.base.BaseFragment;
import com.tadpole.music.view.widget.MyRecyclerViewDivider;
import com.tadpole.music.view.widget.NoScrollViewPager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 各个学院的fragment
 * Created By 孙岩
 * on 2020/9/9
 */
public class CentralMusicFragment extends BaseFragment implements QuestionRankIView, SimulationIView, SpecialPracticeIView {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private UniversityBean.DataBean mParam1;
    private String mParam2;
    //desc
    private TextView tvDesc;
    private RecyclerView rvHead;
    private HeadAdapter headAdapter;
    //模拟套题
    private TabLayout tabLayout;
    //viewpager
    private NoScrollViewPager viewPager;
    //专项练习recyclerView
    private RecyclerView recyclerView;
    //专项练习adapter
    private PracticeAdapter practiceAdapter;

    //viewpager绑定的fragment的集合
    private List<Fragment> fragmentList = new ArrayList<>();
    private TabAdapter tabAdapter;
    //tab集合
    private List<String> tabList = new ArrayList<>();

    private LinearLayout llSimulationPractice;
    private LinearLayout llRealTrain;
    private LinearLayout llOnTrial;
    private LinearLayout llKnowledge;

    //模拟套题
    private LinearLayout llSetOfQuestions;

    //获取等级
    private QuestionRankPresenter questionRankPresenter;
    //获取套题列表
    private SimulationPresenter simulationQuestionPresenter;
    //获取专项练习
    private SpecialPracticePresenter specialPracticePresenter;

    public static CentralMusicFragment newInstance(UniversityBean.DataBean param1, String param2) {
        CentralMusicFragment fragment = new CentralMusicFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, (Serializable) param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_central_music;
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        if (getArguments() != null) {
            mParam1 = (UniversityBean.DataBean) getArguments().getSerializable(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initView(View mRootView) {
        tabLayout = mRootView.findViewById(R.id.tabLayout);
        tvDesc = mRootView.findViewById(R.id.tvDesc);
        tvDesc.setText("说明："+ mParam1.getName()+mParam1.getChief());
        viewPager = mRootView.findViewById(R.id.viewPager);
        recyclerView = mRootView.findViewById(R.id.recyclerView);
        llSimulationPractice = mRootView.findViewById(R.id.llSimulationPractice);
        llRealTrain = mRootView.findViewById(R.id.llRealTrain);
        llOnTrial = mRootView.findViewById(R.id.llOnTrial);
        llKnowledge = mRootView.findViewById(R.id.llKnowledge);
        llSetOfQuestions = mRootView.findViewById(R.id.llSetOfQuestions);

        questionRankPresenter = new QuestionRankPresenter();
        questionRankPresenter.attachView(this);
        questionRankPresenter.getQuestionRank(mParam1.getId());

        simulationQuestionPresenter = new SimulationPresenter();
        simulationQuestionPresenter.attachView(this);

        specialPracticePresenter = new SpecialPracticePresenter();
        specialPracticePresenter.attachView(this);
        specialPracticePresenter.getSpecialPractice(String.valueOf(mParam1.getId()));

        initListeners();
    }

    //初始化监听器
    private void initListeners() {
        //模拟练习
        llSimulationPractice.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), SimulationPracticeActivity.class);
                        intent.putExtra("id", mParam1.getId());
                        intent.putExtra("name", mParam1.getName());
                        startActivity(intent);
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });

        //实战训练
        llRealTrain.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), RealTrainActivity.class);
                        intent.putExtra("id", mParam1.getId());
                        intent.putExtra("name", mParam1.getName());
                        startActivity(intent);
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });

        //免费体验
        llOnTrial.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), FreeExperienceActivity.class);
                        intent.putExtra("id", mParam1.getId());
                        intent.putExtra("name", mParam1.getName());
                        startActivity(intent);
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });

        //重点知识
        llKnowledge.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
//                    if(SpUtil.getInstance(getActivity()).getString(Constant.TOKEN,"").equals("")){
//                        Intent intent = new Intent(getActivity(), LoginActivity.class);
//                        startActivity(intent);
//                    }else {
//                        Intent intent = new Intent(getActivity(), KeyKnowledgeActivity.class);
//                        startActivity(intent);
//                    }
                    ToastUtils.show("敬请期待");
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    //获取等级的回调
    @Override
    public void showQuestionRank(List<QuestionRankBean.DataBean> list) {
        if(list==null||list.size()==0){

        }else{
            tabList.clear();
            for (int i = 0; i < list.size(); i++) {
                tabList.add(list.get(i).getName());
                fragmentList.add(SimulationFragment.newInstance(String.valueOf(mParam1.getId()), String.valueOf(list.get(i).getId())));
            }

            simulationQuestionPresenter.getSimulation(String.valueOf(mParam1.getId()), String.valueOf(list.get(0).getId()), false);

            tabAdapter = new TabAdapter(fragmentList, tabList, getChildFragmentManager(), getActivity());
            viewPager.setAdapter(tabAdapter);
            viewPager.setCurrentItem(0);
            tabLayout.setupWithViewPager(viewPager);

            for (int i = 0; i < tabLayout.getTabCount(); i++) {
                TabLayout.Tab tab = tabLayout.getTabAt(i);
                if (tab != null) {
                    tab.setCustomView(tabAdapter.getTabView(i));
                }

                if (i == 0) {
                    //设置默认进来第一个是选中状态
                    TabLayout.Tab a = tabLayout.getTabAt(0);
                    View view1 = a.getCustomView();
                    if (null != view1 && view1 instanceof TextView) {
                        ((TextView) view1).setTextSize(16);
                        ((TextView) view1).setTextColor(getResources().getColor(R.color.color_33));
                    }
                } else {
                    //设置默认进来第二个是不选中状态
                    TabLayout.Tab b = tabLayout.getTabAt(1);
                    View view2 = b.getCustomView();
                    if (null != view2 && view2 instanceof TextView) {
                        ((TextView) view2).setTextSize(13);
                        ((TextView) view2).setTextColor(getResources().getColor(R.color.color_65));
                    }
                }
            }


            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
            //切换导航菜单时候的监听
            tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    viewPager.setCurrentItem(tab.getPosition(), true);
                    View view = tab.getCustomView();
                    if (null != view && view instanceof TextView) {
                        ((TextView) view).setTextSize(16);
                        ((TextView) view).setTextColor(getResources().getColor(R.color.color_33));
                        ((TextView) view).setTextAppearance(getActivity(), R.style.TabLayoutTextStyle);
                    }

                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                    View view = tab.getCustomView();
                    if (null != view && view instanceof TextView) {
                        ((TextView) view).setTextSize(13);
                        ((TextView) view).setTextColor(getResources().getColor(R.color.color_65));
                    }
                }


                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });
        }

    }

    @Override
    public void show401() {

    }

    @Override
    public void showSimulationQuestion(List<SimulationBean.DataBeanX.DataBean> list) {
        //为了防止RecyclerView与ScrollView嵌套，抢占焦点的问题
        tabLayout.setFocusableInTouchMode(true);
        tabLayout.requestFocus();
//        llSetOfQuestions.setVisibility(View.VISIBLE);
    }

    @Override
    public void showNone() {
        llSetOfQuestions.setVisibility(View.GONE);
    }

    @Override
    public void notifyAdapter() {

    }

    @Override
    public void stopRefresh() {

    }

    //专项练习
    @Override
    public void showSpecialPracticeList(final List<SpecialPracticeBean.DataBean> list) {
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
        recyclerView.setLayoutManager(layoutManager);
        //创建适配器
        practiceAdapter = new PracticeAdapter(getActivity(), list);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(practiceAdapter);
        recyclerView.setNestedScrollingEnabled(false);

        practiceAdapter.setOnChildClickListener(new BaseAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(View v, int position) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), SpecialExercisesActivity.class);
                        intent.putExtra("college_id",String.valueOf(list.get(position).getCollege_id()));
                        intent.putExtra("rank_id",String.valueOf(list.get(position).getId()));
                        intent.putExtra("name",mParam1.getName());
                        startActivity(intent);
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }
}